/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.zprofilefrontend.model;

import com.vng.zing.zprofilefrontend.common.Util;
import com.vng.zing.calc.thrift.Profile;
import com.vng.zing.calc.thrift.ProfileSession;
import com.vng.zing.logger.ZLogger;
import com.vng.zing.stats.Profiler;
import com.vng.zing.stats.ThreadProfiler;
import com.vng.zing.stats.monitor.common.TemplateManager;
import static com.vng.zing.zprofilefrontend.model.BaseModel.checkLogIn;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author namhh
 */
public class UpdateModel extends BaseModel {

    private static final Logger _Logger = ZLogger.getLogger(UpdateModel.class);
    public static final UpdateModel Instance = new UpdateModel();

    private UpdateModel() {
    }

    @Override
    public void process(HttpServletRequest req, HttpServletResponse resp) {
        ThreadProfiler profiler = Profiler.getThreadProfiler();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = null;
        try {
            out = resp.getWriter();
            ///TODO..
            profiler.push(this.getClass(), "output");
            ProfileSession profileSession = checkLogIn(req);
            if (null != profileSession) {
                /////////////
                TemplateDataDictionary dic = TemplateDictionary.create();               
                Profile profile = getClientWithCache().getaLoad(profileSession.getId());
                 setHeaderFooter("ZProfile - Update", dic, profile);
                dic.setVariable("USERNAME", profile.getUsername());
                dic.setVariable("DISPLAY_NAME", profile.displayName);
                dic.setVariable("EMAIL", profile.getEmail());
                dic.setVariable("MOBILE", profile.mobileNumber);
                dic.setVariable("DATE_OF_BIRTH", Util.toDate(profile.getDateOfBirth()));
                for (Map.Entry< Short, String> entry : CityModel.getInstance().getAllCities().entrySet()) {
                    TemplateDataDictionary sliceCities = dic.addSection("SLICE_CITY");
                    sliceCities.setVariable("CITY_ID", String.format("%s", entry.getKey()));
                    sliceCities.setVariable("CITY", entry.getValue());
                    if (entry.getKey().equals(profile.getCityId())) {
                        sliceCities.setVariable("SELECTED", "selected");
                    }
                }
                Template home = TemplateManager.getTemplate("update");
                out.println(home.renderToString(dic));
                ////////////               

            } else {
                resp.sendRedirect("/login");
            }
            profiler.pop(this.getClass(), "output");
        } catch (Exception ex) {
            _Logger.error(null, ex);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

}
